//
//  SearchNet.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/22.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "SearchNet.h"
#import "BasicNetManager.h"
#import "BLHeader.h"
#import "Partner.h"
#import <YYModel.h>

@implementation SearchNet

+ (void)searchPartner:(NSString *)tel completer:(void(^)(id result, NSError *error))completer {
    NSString *userClass = [[NSUserDefaults standardUserDefaults] objectForKey:USERCLASS];
    NSDictionary *param = @{ ([userClass integerValue] == 1 ?  @"saleTel"  :  @"userTel") : tel};
    NSString *url = [userClass integerValue] == 1 ? SELECTSALE : SELECTSTORE;
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:url param:param completer:^(id result, NSError *error) {
        NSArray *array;
        if (result) {
            Partner *partner = [Partner yy_modelWithJSON:result];
            array = @[partner];
        }
        if(completer) {
            completer(array, error);
        }
    }];
}

#pragma mark 确定添加生成二维码
+ (void)commitAddPartner:(NSString *)tel rate:(float)rate completer:(void (^)(id result, NSError *error))completer {
    NSString *userClass = [[NSUserDefaults standardUserDefaults] objectForKey:USERCLASS];
    NSString *userTel = [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
    
    NSString *saleTel = [userClass integerValue] == 1 ?  tel : userTel;
    
    NSString *storeTel = [userClass integerValue] == 1 ? userTel : tel;
    NSDictionary *param = @{ @"userTel" : storeTel, @"saleTel" : saleTel, @"ratio" : [NSString stringWithFormat:@"%f",rate]};
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:CREATE2CODE param:param completer:^(id result, NSError *error) {
        if(completer) {
            completer(result, error);
        }
    }];
}

#pragma mark 扫二维码同意添加
+ (void)scanAddPartner:(NSString *)sacnResult completer:(void(^)(id result, NSError *error))completer {
    NSArray *components = [sacnResult componentsSeparatedByString:@"#"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    for (NSString *str in components) {
        NSArray *param = [str componentsSeparatedByString:@":"];
        [params setObject:param[1] forKey:param[0]];
    }
    
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:SCANADDPARTNER param:params completer:^(id result, NSError *error) {
        if(completer) {
            Partner *parter;
            if(result) {
                parter = [Partner yy_modelWithJSON:result];
            }
            completer(parter, error);
        }
    }];
}

#pragma Mark 获取店主/销售
+ (void)saleOrStoreList:(void(^)(id result, NSError *error))completer {
    NSString *userClass = [[NSUserDefaults standardUserDefaults] objectForKey:USERCLASS];
    NSString *url = [userClass integerValue] == 1 ? SELECTSALELIST : SELECTSTORELIST;
    NSString *telKey = [userClass integerValue] == 1 ? @"userTel" : @"saleTel";
    NSString *tel = [[NSUserDefaults standardUserDefaults] objectForKey:USERTEL];
    
    NSDictionary *param = @{telKey : tel};
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:url param:param completer:^(id result, NSError *error) {
        NSInteger count = [result count];
        NSMutableArray *results = [[NSMutableArray alloc] init];
        for (int i = 0; i < count; i++) {
            if ([result[i] isKindOfClass:[NSDictionary class]]){
                Partner *partner = [Partner yy_modelWithJSON:result[i]];
                [results addObject:partner];
            }
        }
        if(completer) {
            completer([results copy], error);
        }
    }];
}

#pragma mark 第一次微信登录 要求用户输入手机号码
+ (void) firstWechatLogin:(NSString *)tel completer:(void(^)(id result, NSError *error))completer {
    NSDictionary *dic = @{@"userTel" : tel};
    BasicNetManager *manager = [BasicNetManager share];
    [manager postURL:FIRSTWECHATLOGIN param:dic completer:^(id result, NSError *error) {
        if(completer) {
            completer(result, error);
        }
    }];
}

@end
